package com.moli.iov.open.web.controller;

import com.moli.iov.exception.BusinessException;
import com.moli.iov.open.constants.Constant;
import com.moli.iov.open.model.vo.req.BaseVo;
import com.moli.iov.rest.GenericCode;
import com.moli.iov.sys.model.dto.res.ClientDetailsResDto;
import com.moli.iov.util.CommonUtils;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 *
 * </p>
 *
 * @author zql
 * @since 2019/9/3 15:01
 */
@Slf4j
public class BaseController {

  @Autowired
  HttpServletRequest request;

  /**
   * 获取企业信息
   * @return
   */
  public BaseVo getBaseVo(){

    Object object = request.getAttribute(Constant.DEFAULT_REQUEST_CLIENT);
    if(object == null){
      throw new BusinessException(GenericCode.TOKEN_FAIL.getCode(), GenericCode.TOKEN_FAIL.getMessage());
    }

    ClientDetailsResDto clientDetailsResDto = (ClientDetailsResDto) object;
    if(clientDetailsResDto == null){
      throw new BusinessException(GenericCode.TOKEN_FAIL.getCode(), GenericCode.TOKEN_FAIL.getMessage());
    }

    BaseVo baseVo = new BaseVo();
    baseVo.setCompanyId(clientDetailsResDto.getCompanyId());
    baseVo.setCompanyName(clientDetailsResDto.getCompanyName());
    baseVo.setAppId(clientDetailsResDto.getAppId());
    baseVo.setPageNum(null);
    baseVo.setPageSize(null);

    return baseVo;
  }

  /**
   * 请求企业信息
   * @param baseVo
   * @return
   */
  public BaseVo assembleUserInfo(BaseVo baseVo){
    BaseVo baseVo1 = getBaseVo();
    CommonUtils.copyProperties(baseVo1, baseVo);
    return baseVo;
  }
}
